package service;

import dao.NewsDao;
import dao.NewsDaoImpl;
import pojo.News;
import pojo.NewsPage;

import java.util.List;
import java.util.Map;

/**
 * @author 郝雷蒙
 * @Description
 * @date 2020/11/10 14:43
 */
public class NewsServiceImpl implements NewsService{
    private NewsDao  newsDao = new NewsDaoImpl();

    @Override
    public NewsPage getAllnews(Map<String, Object> params) {
        Integer page = (Integer) params.get("page");
        Integer size = (Integer) params.get("size");

        Integer start = (page - 1)*size;

        params.put("start",start);
        params.put("length",size);

        List<News> news = newsDao.selectAllNews(params);
        Integer count = newsDao.selectNewsCount(params);


        Integer total = (count%size==0)? count/size : count/size+1;

        NewsPage newsPage = new NewsPage();
        newsPage.setNewsList(news);
        newsPage.setRowCount(count);
        newsPage.setPageCount(total);
        newsPage.setPageNum(page);
        newsPage.setPageSize(size);

        return newsPage;
    }

    @Override
    public Integer updateViewCount(Integer count, Integer id) {

        return newsDao.updateViewCount(count,id);
    }

    @Override
    public News selectOneById(Integer id) {
        return newsDao.selectOneById(id);
    }
}
